package io.github.shang11925.shangweb2.day240908;


import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/language")
public class MyBrowserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String acceptLanguage = req.getHeader("Accept-Language");
        String[] languages = acceptLanguage.split(",");
        String language = languages[0];
        if ("zh-CN".equals(language)) {
            resp.getWriter().println("""
                    <!DOCTYPE html>
                    <html lang="en">
                    <head>
                        <meta charset="UTF-8">
                        <title>Title</title>
                    </head>
                    <body>
                    <h1>肚子饿了</h1>
                    </body>
                    </html>
                    """);
        }
        else if ("en".equals(language)) {
            resp.getWriter().println("""
                    <!DOCTYPE html>
                    <html lang="en">
                    <head>
                        <meta charset="UTF-8">
                        <title>Title</title>
                    </head>
                    <body>
                    <h1>hello world!</h1>
                    </body>
                    </html>
                    """);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
